﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Core;
using Tesla.Math;

namespace Tesla.Scene.Extension {
    /// <summary>
    /// Controller that rotates the Spatial it is attached to each frame.
    /// </summary>
    public sealed class RotateController : ISpatialController {
        private bool _isEnabled;
        private float _angle;
        private float _increment;
        private Vector3 _axis;

        /// <summary>
        /// Gets or sets if this controller should be enabled.
        /// </summary>
        public bool IsEnabled {
            get { 
                return _isEnabled;
            }
            set {
                _isEnabled = value;
            }
        }

        /// <summary>
        /// Creates a new RotateController instance.
        /// </summary>
        /// <param name="axis">Axis to rotate the Spatial about</param>
        /// <param name="increment">Increment, in degrees, per frame in which to rotate the Spatial.</param>
        public RotateController(Vector3 axis, float increment) {
            _isEnabled = true;
            _axis = axis;
            _increment = increment;
        }

        /// <summary>
        /// Update the controller.
        /// </summary>
        /// <param name="time">Interpolation time</param>
        /// <param name="caller">Spatial that called this controller</param>
        public void Update(GameTime time, Spatial caller) {
            if(_isEnabled) {
                //Increment rotation angle
                _angle += _increment * (float) time.ElapsedGameTime.TotalSeconds;
                if(_angle > 360){
                    _angle = 0;
                }
                //Create quaternion from the axis of rotation and the angle
                Quaternion q = Quaternion.FromAngleAxis(MathHelper.ToRadians(_angle), _axis);
                //Update the caller's rotation
                caller.Rotation = q;
            }
        }
    }
}
